package org.dizena.base.dao;

import org.apache.commons.lang3.StringUtils;
import org.dizena.base.avo.req.JqGridReq;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public abstract class BaseDao
{

    public Query idQuery(String id)
    {
        return new Query(Criteria.where("id").is(id));
    }

    public Query keyIsVal(String key, Object val)
    {
        return new Query(Criteria.where(key).is(val));
    }

    public Query keyIsVal(String[] keys, Object[] vales)
    {
        if (keys != null && keys.length > 0 && keys.length == vales.length)
        {
            Criteria c = Criteria.where(keys[0]).is(vales[0]);
            for (int i = 1; i < vales.length; i++)
            {
                c = c.and(keys[i]).is(vales[i]);
            }
            return new Query(c);
        }
        return new Query();
    }

    public Query map2QueryLike(Map<String, Object> map, String searchField)
    {
        Criteria criteria = null;
        if (map != null && map.size() > 0)
        {
            Set<Entry<String, Object>> sets = map.entrySet();
            int i = 0;
            for (Entry<String, Object> entry : sets)
            {
                String key = entry.getKey();
                Object val = entry.getValue();
                if (key.equals(searchField))
                {
                    String seal = (String) val;
                    if (i == 0)
                    {
                        criteria = Criteria.where(searchField).regex(seal);
                    }
                    else
                    {
                        criteria = criteria.and(searchField).regex(seal);
                    }
                }
                else
                {
                    // other
                    if (i == 0)
                    {
                        criteria = Criteria.where(key).is(val);
                    }
                    else
                    {
                        criteria = criteria.and(key).is(val);
                    }
                }

                i++;
            }
        }
        Query query = new Query();
        if (criteria != null)
        {
            query = new Query(criteria);
        }
        return query;
    }

    public Query allQuery(@NotNull JqGridReq req, Map<String, Object> map)
    {
        Long page = req.getPage();
        Integer size = req.getSize();
        String ascDesc = req.getAscDesc();
        String sortField = req.getSortField();
        String searchField = req.getSearchField();
        Query query = map2QueryLike(map, searchField);

        if (page != null && size != null)
        {
            query.skip((page - 1) * size);
            query.limit(size);
        }
        if (StringUtils.isNotEmpty(ascDesc) && StringUtils.isNotEmpty(sortField))
        {
            Sort.Direction dire = Sort.Direction.DESC;
            if ("asc".equalsIgnoreCase(ascDesc))
            {
                dire = Sort.Direction.ASC;
            }
            Sort sort = Sort.by(dire, sortField);
            query.with(sort);
        }

        return query;
    }
}

